package com.rambo.baselib.provider;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.HashMap;
import java.util.Map;

public class BaseViewHolder {
    private Map<Integer, Component> viewMap;

    public Component convertView;

    public BaseViewHolder(Component convertView) {
        this.viewMap = new HashMap<>();
        this.convertView = convertView;
    }
    public <T extends Component> T getView( int viewId ){
       Component component=viewMap.get(viewId);
       if(component==null){
           component= convertView.findComponentById(viewId);
           if(component==null){
               return null;
           }
           viewMap.put(viewId,component);
       }
       return (T)component;
    }

    public BaseViewHolder setText( int viewId, String value) {
        Text view = getView(viewId);
        view.setText(value);
        return this;
    }

    public BaseViewHolder setText( int viewId,  int strId) {
        Text view = getView(viewId);
        view.setText(strId);
        return this;
    }


    public BaseViewHolder setImageResource( int viewId,  int imageResId) {
        Image view = getView(viewId);
        view.setPixelMap(imageResId);
        return this;
    }


    public BaseViewHolder setBackgroundColor( int viewId,  String color) {
        Component view = getView(viewId);
        ShapeElement shapeElement=  new ShapeElement();
        shapeElement.setRgbColor(new RgbColor(Color.getIntColor(color)));
        view.setBackground(shapeElement);
        return this;
    }


    public BaseViewHolder setBackgroundRes(int viewId, Element element) {
        Component view = getView(viewId);
        view.setBackground(element);
        return this;
    }


    public BaseViewHolder setTextColor( int viewId,  String textColor) {
        Text view = getView(viewId);
        view.setTextColor(new Color(Color.getIntColor(textColor)));
        return this;
    }
    public BaseViewHolder setTextColor( int viewId,  Color color) {
        Text view = getView(viewId);
        view.setTextColor(color);
        return this;
    }

    public BaseViewHolder setVisibility(int viewId, int visibility) {
        Component view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }
}
